package com.test.qpbgboot.base.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class AlipayConfig {

    @Value("${alipay.app_id}")
    private String appId;
    @Value("${alipay.private_key}")
    private String privateKey;
    @Value("${alipay.ali_public_key}")
    private static String aliPublicKey;
    @Value("${alipay.sign_type}")
    private static String signType;
    @Value("${alipay.charset}")
    private static String charset;
    @Value("${alipay.gatewayUrl}")
    private String gatewayUrl;

    @Bean
    public AlipayClient alipayClient(){
        return new DefaultAlipayClient(
                gatewayUrl,
                appId,
                privateKey,
                "json",
                charset,
                aliPublicKey,
                signType
        );
    }

    public static boolean aliSignature(Map<String, String> params) throws AlipayApiException {
        return AlipaySignature.rsaCheckV1(params, aliPublicKey, charset, signType);
    }
}
